﻿using SportExamTool.CommonHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SportExamTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
          
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool flag = true;
            if (string.IsNullOrWhiteSpace(machineCode.Text))
            {
                machineCodeError.Text = "机器码不能为空";
                flag = false;
            }
            if (isAlways.CheckState==CheckState.Unchecked && string.IsNullOrWhiteSpace(expireDate.Text))
            {
                machineCodeError.Text = "有效期不能为空";
                flag = false;
            }
            
            if (!flag)
            {
                return;
            }
            int checkState = (int)isAlways.CheckState;
            string authorizeCodeValue = CreateEncrypt(machineCode.Text, expireDate.Text, checkState);
            authorizeCode.Text = authorizeCodeValue; 
        }

        /// <summary>
        /// 将信息进行加密
        /// </summary>
        /// <param name="machineCode"></param>
        /// <param name="expireDate"></param>
        /// <returns></returns>
        public string CreateEncrypt(string machineCode,string expireDate,int isAlways)
        {
            if (string.IsNullOrWhiteSpace(machineCode)||(string.IsNullOrWhiteSpace(expireDate)&& isAlways!=1))
                return "条件不能为空";
            if (isAlways==1) expireDate = "";

            var model = new AuthorizeModel { MachineCode = machineCode, ExpireDate = expireDate, IsAlways = isAlways };
            string json= JsonSerializer.Serialize(model);
            string authorizeCode = json.ToAes128EncryptString();

            return authorizeCode;
        }

        /// <summary>
        /// 将信息进行信息解密
        /// </summary>
        /// <param name="machineCode"></param>
        /// <param name="expireDate"></param>
        /// <returns></returns>
        public AuthorizeModel CreateDecrypt(string authorizeCode)
        {
            AuthorizeModel model = new AuthorizeModel() ;
            if (string.IsNullOrWhiteSpace(authorizeCode))
            {
                model.MachineCode = "授权码不能为空";
                return model;
            }
           

            var str = authorizeCode.ToAes128DecryptString();
            if (string.IsNullOrWhiteSpace(str))
            {
                model.MachineCode = "解密失败";
                return model;
            }
              

             model = JsonSerializer.Deserialize<AuthorizeModel>(str);
            if (model == null)
            {
                model.MachineCode = "数据转换失败";
                return model;
            } 
            return model;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            authorizeCode.Text = "";
        }

        private void authorizeCode_TextChanged(object sender, EventArgs e)
        {

        }

        private void expireDate_ValueChanged(object sender, EventArgs e)
        {
            this.expireDate.Format = DateTimePickerFormat.Long;
            this.expireDate.CustomFormat = null;
        }

        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(authorizeCode.Text))
            {
                authorizeCodeError.Text = "授权码不能为空";
                return;
            }

            var model = CreateDecrypt(authorizeCode.Text);
            this.machineCode.Text = model.MachineCode;
            this.isAlways.Checked = model.IsAlways==1?true:false;
            this.expireDate.Text = model.ExpireDate;
        }
    }
}
